/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package User;

import Bean.myPhotoBeanRemote;
import Bean.userBeanRemote;
import Objects.Album;
import java.io.IOException;
import java.util.List;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 *
 * @author ntfsbk
 */
public class LoginController extends MultiActionController {

    InitialContext context;

    public ModelAndView hello(HttpServletRequest request,
            HttpServletResponse response) {

        ModelAndView view = new ModelAndView("login");
        return view;

    }

    public void checkLogin(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        try {
            context = new InitialContext();
            userBeanRemote userBean = (userBeanRemote) context.lookup("userBean");

            String UserName = request.getParameter("user");
            String Passwd = request.getParameter("pass");

            Boolean result = userBean.checkLogin(UserName, Passwd);

            response.setContentType("html/textplain");
            response.getWriter().write(result.toString());
            HttpSession session = request.getSession();
            session.setAttribute("userName", UserName);
            int userId = userBean.getUserIdByUserName(UserName);
            session.setAttribute("userId", userId);
        } catch (Exception e) {
        }
    }

    public ModelAndView signUp(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("signUp");
    }

    public void checkExistedUser(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            context = new InitialContext();
            userBeanRemote userBean = (userBeanRemote) context.lookup("userBean");

            String UserName = request.getParameter("user");

            Boolean result = userBean.checkExistedUser(UserName);
            response.getWriter().write(result.toString());
        } catch (Exception e) {
        }
    }

    public void checkEmail(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            context = new InitialContext();
            userBeanRemote userBean = (userBeanRemote) context.lookup("userBean");

            String email = request.getParameter("user");

            Boolean result = userBean.checkExistedUser(email);
            response.getWriter().write(result.toString());
        } catch (Exception e) {
        }
    }

    public void insertNewUser(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            context = new InitialContext();
            userBeanRemote userBean = (userBeanRemote) context.lookup("userBean");

            String userName = request.getParameter("user");
            String passWord = request.getParameter("pass");
            String email = request.getParameter("email");
            String gender = request.getParameter("gender");

            String month = request.getParameter("month");
            String day = request.getParameter("day");
            String year = request.getParameter("year");
            String birth = year + "-" + month + "-" + day;

            Boolean result = userBean.insertNewUser(userName, passWord, email, gender, birth);

            response.getWriter().write(result.toString());
            if (result == true) {
                HttpSession session = request.getSession();
                session.setAttribute("userName", userName);
                int userId = userBean.getUserIdByUserName(userName);
                session.setAttribute("userId", userId);
            }
        } catch (Exception e) {
            response.getWriter().write("false");
        }
    }

    public ModelAndView block(HttpServletRequest request,
            HttpServletResponse response) {
        ModelAndView view = new ModelAndView("block");
        String expirationDateStr = request.getParameter("expirationDateStr");
        view.addObject("expirationDateStr", expirationDateStr);

        return view;
    }

    public ModelAndView logoutAction(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.invalidate();
        //session = request.getSession(false);
        //response.getWriter().write("Session : " + session);

        ModelAndView view = new ModelAndView("login");
        return view;

    }
}
